﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Tools
{
    public partial class Tool
    {
        /// <summary>
        /// 分页，
        /// Author:HLX
        /// Create Date:2008-01-09
        /// </summary>
        /// <param name="mo_page">分页实体</param>
        /// <returns></returns>
        public static DataTable GetPagesEasy(MO_easyPages Mo_page)
        {
            SqlParameter[] parms=
             {
                 MakeInParam("@Table",SqlDbType.NVarChar,100,Mo_page.TableName),
                 MakeInParam("@fieldList",SqlDbType.NVarChar,1000,Mo_page.Fields),
                 MakeInParam("@Condition",SqlDbType.NVarChar,500,Mo_page.Condition),
                 MakeInParam("@orderExpression",SqlDbType.NVarChar,200,Mo_page.OrderExpression),
                 MakeInParam("@pageSize",SqlDbType.Int,4,Mo_page.PageSize),
                 MakeInParam("@pageIndex",SqlDbType.Int,4,Mo_page.PageIndex),
                 MakeOutParam("@count",SqlDbType.Int,4)
             };
            try
            {
                DataTable dt = ExecProcDataTable("PR_easyPagination", parms);
                Mo_page.Count = int.Parse(parms[6].Value.ToString());
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 传入输入参数
        /// </summary>
        /// <param name="ParamName">存储过程名称</param>
        /// <param name="DbType">参数类型</param>
        /// <param name="Size">参数大小</param>
        /// <param name="Value">参数值</param>
        /// <returns>新的 parameter 对象</returns>
        public static  SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
        {
            #region
            return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);

            #endregion
        }

        /// <summary>
        /// 生成存储过程参数
        /// </summary>
        /// <param name="ParamName">存储过程名称</param>
        /// <param name="DbType">参数类型</param>
        /// <param name="Size">参数大小</param>
        /// <param name="Direction">参数方向</param>
        /// <param name="Value">参数值</param>
        /// <returns>新的 parameter 对象</returns>
        public static  SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
        {

            SqlParameter param;
            if (Size > 0)
            {
                param = new SqlParameter(ParamName, DbType, Size);
            }
            else
            {
                param = new SqlParameter(ParamName, DbType);
            }

            param.Direction = Direction;
            if (!(Direction == ParameterDirection.Output && Value == null))
            {
                param.Value = Value;
            }
            return param;

        }
        /// <summary>
        /// 传入返回值参数
        /// </summary>
        /// <param name="ParamName">存储过程名称</param>
        /// <param name="DbType">参数类型</param>
        /// <param name="Size">参数大小</param>
        /// <returns>新的 parameter 对象</returns>
        public static  SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
        {
            #region
            return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
            #endregion
        }

        /// <summary>
        ///   返回存储过程执行后DataTable对象 
        /// </summary>
        /// <param name="procName">存储过程的名称</param>
        /// <param name="parms">存储过程所需参数</param>
        /// <returns></returns>
        public static  DataTable ExecProcDataTable(string procName, SqlParameter[] parms)
        {

            //需要自己补充
            return new DataTable();
        }

        /*
         
USE [FDAYSAccount]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[PR_easyPagination]
(
@Table nvarchar(100),
@fieldList nvarchar(1000),
@Condition nvarchar(500),
@orderExpression nvarchar(200),
@pageSize int,
@pageIndex int,
@count int output
)
as
begin
declare @sql nvarchar(1000)
declare @orderExpressionInverted nvarchar(200)

set @orderExpression= ' order by '+@orderExpression
set @Condition=' where '+@Condition
set @orderExpressionInverted=replace(@orderExpression,' asc',' @@asc')        --要做更严格的匹配，避免字段头包括asc,desc
set @orderExpressionInverted=replace(@orderExpressionInverted,' desc',' asc')
set @orderExpressionInverted=replace(@orderExpressionInverted,' @@asc',' desc')

select @sql='select '+ @fieldList +' from (select top '+str(@pageSize) + @fieldList + ' from (select top '+str(@pageSize*@pageIndex) +' '+ @fieldList +' from '+ @Table+' '+@Condition+ ' '+@orderExpression+
') tempTable '+@orderExpressionInverted+') temptable2 '+@orderExpression
exec(@sql);
set @sql='select @count = count(*) from '+@Table +' '+@Condition;
exec sp_executesql @sql,N'@count int output',@count output
end
         
         */

    }
}
